package com.blacksheep.mtm.controller;

import com.blacksheep.mtm.common.utils.ResultUtil;
import com.blacksheep.mtm.pojo.common.DataTablesResult;
import com.blacksheep.mtm.pojo.common.Result;
import com.blacksheep.mtm.pojo.manager.TbOrderShipping;
import com.blacksheep.mtm.service.OrderShippingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 运单接口
 */
@RestController
@Api(description= "运单管理")
public class OrderShippingController {


    @Autowired
    private OrderShippingService shippingService;

    @RequestMapping(value = "/shipping/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取运单列表")
    public DataTablesResult getOrderShippingList(int draw, int start, int length, String searchKey, int[] state,
                                         String minDate, String maxDate,
                                         @RequestParam("search[value]") String search,
                                         @RequestParam("order[0][column]") int orderCol,
                                         @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","shipping_id", "order_id","delivery_id", "receiver_name", "receiver_phone", "address_type", "receiver_state", "receiver_address","create_time","update_time"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "create_time";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }
        if (!StringUtils.isBlank(search)){
            searchKey =search;
        }
        DataTablesResult result= shippingService.getList(draw,start,length,state,searchKey,minDate,maxDate,orderColumn,orderDir);
        return result;
    }


    @RequestMapping(value = "/shipping/update",method = RequestMethod.GET)
    @ApiOperation(value = "修改运单信息")
    public Result<TbOrderShipping> updateShipping(TbOrderShipping shipping){
        shippingService.updateShipping(shipping);
        return new  ResultUtil<TbOrderShipping>().setData(null);
    }
}
